package com.isoft.c2team3service2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isoft.c2team3service2.entity.UserFollowArtist;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface UserFollowArtistMapper extends BaseMapper<UserFollowArtist> {

    @Select("SELECT COUNT(1) FROM user_follow_artist WHERE user_id = #{userId} AND artist_id = #{artistId}")
    int checkFollowStatus(@Param("userId") Integer userId, @Param("artistId") Integer artistId);

    @Insert("INSERT INTO user_follow_artist (user_id, artist_id, create_time) VALUES (#{userId}, #{artistId}, #{createTime})")
    void insertFollowArtist(@Param("userId") Integer userId, @Param("artistId") Integer artistId, @Param("createTime") LocalDateTime createTime);

    @Delete("DELETE FROM user_follow_artist WHERE user_id = #{userId} AND artist_id = #{artistId}")
    int deleteFollowArtist(@Param("userId") Integer userId, @Param("artistId") Integer artistId);

    //    @Select("SELECT artist_id FROM user_follow_artist WHERE user_id = #{userId}")
    @Select("SELECT u.artist_id,l.nick_name FROM user_follow_artist u,tb_person l WHERE u.user_id = #{userId} and l.id=#{userId}")
    List<Integer> selectFollowedArtistIds(@Param("userId") Integer userId);
    //@Select("SELECT u.artist_id, l.nick_name " +
//        "FROM user_follow_artist u " +
//        "LEFT JOIN tb_person l ON #{userId} = l.id " +  // 关键修复：用艺人ID关联用户表
//        "WHERE u.user_id = #{userId}")
//
    @Select("SELECT u.artist_id, p.nick_name " +
            "FROM user_follow_artist u " +
            "JOIN artist a ON u.artist_id = a.id " +
            "JOIN tb_person p ON a.user_id = p.id " +
            "WHERE u.user_id = #{userId}")
// 返回值改为List<Map>，同时包含artist_id和nick_name
    List<Map<String, Object>> selectFollowedArtistsWithNickname(@Param("userId") Integer userId);
}